// 获取折线图数据
const getLineSeries = (dataList) => {
	console.log('啊', dataList)
	let series = []
	const colors = [
		'#2b908f',
		'#7cb5ec',
		'#e4d354',
		'#d34348',
		'#f7a35c',
		'#8085e9',
		'#f15c80',
		'#f00',
		'#f45b5b',
		'#91e8e1',
		'#40E0D0'
	]

	Object.keys(dataList).forEach(key => {

		if (key.includes('臭氧')) {
			series.push(

				{
					type: 'line',
					name: key,
					data: dataList[key],
					symbol: 'circle',
					showSymbol: false,
					symbolSize: 10,
					connectNulls: true,
					smooth: true,
					color: colors[Object.keys(dataList).indexOf(key)],
					yAxisIndex: 0,
					lineStyle: {
						width: 2,
						color: colors[Object.keys(dataList).indexOf(key)]
					}
				})
		}




	});
	series.push({
		type: 'line',
		name: 'nox浓度',
		data: dataList['nox浓度'],
		symbol: 'circle',
		showSymbol: false,
		symbolSize: 10,
		connectNulls: true,
		smooth: true,
		color: colors[11],
		xAxisIndex: 1,
		yAxisIndex: 1,
		lineStyle: {
			width: 2,
			color: colors[11]
		}
	})


	return series
}
// 获取背景图数据
const getExtraSeries = (dataList, xAxisData) => {
	const colors = ['#E6E6E6', '#CBCBCB', '#AFAFAF', '#707070', '#B7B7B7']
	let series = []
	const seriesColors = dataList
	let keysIndex = 0
	const seriesMap = {}
	for (let i in seriesColors) {
		seriesMap[keysIndex + '|' + i] = seriesColors[i]
		keysIndex++
	}
	const keys = Object.keys(seriesMap)
	if (keys.length === 0) return series
	// const values = Object.values(seriesMap);
	const len = seriesMap[keys[0]].length
	let newarr = []
	let tempList = {},
		tempValue = '',
		tempIndex = 0
	for (let i = 0; i < len; i++) {
		for (let j = 0; j < keys.length; j++) {
			const key = keys[j]
			if (seriesMap[key][i] !== null) {
				newarr[i] = key
				break
			}
		}
	}
	for (let i = 0; i < len; i++) {
		const key = newarr[i]
		const time = xAxisData[i]
		if (time) {
			if (i === 0) {
				tempValue = key
				tempList[tempIndex + '|' + tempValue] = {
					startTime: time
				}
			} else if (i === len - 1) {
				tempList[tempIndex + '|' + tempValue].endTime = time
			} else {
				const lastKey = newarr[i - 1]
				if (lastKey != key) {
					tempList[tempIndex + '|' + tempValue].endTime = time
					tempValue = key
					tempIndex++
					tempList[tempIndex + '|' + tempValue] = {
						startTime: time
					}
				}
			}
		}
	}
	let colorIndex = 0
	const colorMap = {}
	for (let i in tempList) {
		const info = tempList[i]
		const textarr = i.split('|')
		const text = textarr[textarr.length - 1]
		if (text && text !== 'undefined' && info.startTime && info.endTime) {
			let markcolor = colorMap[text]
			if (!markcolor) {
				markcolor = colors[colorIndex]
				colorMap[text] = markcolor
			}
			series.push({
				type: 'line',
				name: text,
				data: new Array(len).fill(''),
				symbol: 'circle',
				showSymbol: false,
				symbolSize: 10,
				connectNulls: true,
				smooth: true,
				color: markcolor,
				lineStyle: {
					width: 0,
					color: '#fff'
				},
				xAxisIndex: 1,
				yAxisIndex: 1,
				markArea: {
					silent: true,
					data: [
						[{
								name: text,
								xAxis: info.startTime,
								itemStyle: {
									color: markcolor
								}
							},
							{
								xAxis: info.endTime
							}
						]
					]
				}
			})
			colorIndex++
		}
	}
	return series
}

// 获取X轴P分组
const getPlist = (pList) => {

	const maps = {}
	const groupSeparates = []
	let list = []
	const allList = {}
	let tempList = [],
		tempValue = '',
		tempIndex = 0
	for (let i = 0; i < pList.length; i++) {
		const data = pList[i]
		if (data !== tempValue) {
			tempValue = data
			tempIndex++
			allList[tempIndex + '|' + data] = []
		}
		allList[tempIndex + '|' + data].push(data)
		if (i === 0) {
			groupSeparates[i] = true
		} else if (i > 0 && data != pList[i - 1]) {
			groupSeparates[i] = true
		} else {
			groupSeparates[i] = false
		}
	}

	for (let i in allList) {
		const data = allList[i]
		const len_middle = Math.floor(data.length / 2)

		const new_list = data.map((item, key) => {




			return key === len_middle ? (item === 1 ? '上升' : item === 2 ? '下降' : item === 3 ? '平稳' : '') :''
		})
		list = list.concat(new_list)
	}
	return {
		gridIndex: 1,
		data: list,
		position: 'bottom',
		axisTick: {
			length: 50,
			interval: function(index, value) {
				return groupSeparates[index] // 根据标识分组的刻度线
			}
		},
		axisLabel: {
			fontSize: 20,
			margin: 30,
			interval: function(index, value) {
				return list[index] !== '' // 根据标识分组的刻度线
			}, // 显示所有标签
			rich: {
				offset: {
					width: 0
				}
			}
		}

	}
}
// 获取X轴
const getxAxis = (xAxisData, pList) => {
	let zoomStart = 0
	let zoomEnd = 100
	var xAxis = [{
			type: 'category',
			boundaryGap: false,
			axisLine: {
				onZero: true
			},
			data: xAxisData,
			splitNumber: xAxisData.length,

		},
		{
			gridIndex: 1,
			data: xAxisData,
			boundaryGap: false,
			axisLine: {
				onZero: true
			},

			splitNumber: xAxisData.length,
			axisLabel: {
				margin: 10 // 标签到轴线的距离
			},
			position: 'bottom'
		},

	]
	if (pList && pList.length) {
		xAxis.push(getPlist(pList))
	}
	var dataZoom = [{
			show: true,
			realtime: true,
			height: 10,
			xAxisIndex: [0],
			start: zoomStart,
			end: zoomEnd,
			bottom: '2%'
		},
		{
			type: 'inside',
			realtime: true,
			start: zoomStart,
			end: zoomEnd,
			xAxisIndex: [0]
		}
	]
	for (var i = 0; i < xAxis.length; i++) {
		dataZoom[0].xAxisIndex.push(i) // 填充datazoom的联动
		dataZoom[1].xAxisIndex.push(i) // 填充datazoom的联动
	}
	return {
		xAxis,
		dataZoom
	}
}
// 获取Y轴
const getyAxis = () => {
	return [{

			name: '臭氧浓度',
			type: 'value',
			min: 'dataMin',
			max: 'dataMax',
			min: function(value) { // 取最小值向下取整为最小刻度
				let num = 1
				const len = value.min.toString().length
				if (len > 2) {
					num = 10 * (len - 2)
				} else if (len === 2) {
					num = 10 * (len - 1)
				}
				return Math.floor(value.min / num) * num
			},
			max: function(value) { // 取最大值向上取整为最大刻度
				let num = 1
				const len = value.max.toString().length
				if (len > 2) {
					num = 10 * (len - 2)
				} else if (len === 2) {
					num = 10 * (len - 1)
				}
				return Math.ceil(value.max / num) * num
			},
			// splitNumber: 20,

		},

		{
			name: 'nox浓度',
			gridIndex: 1,
			type: 'value',
			min: 'dataMin',
			max: 'dataMax',
			min: function(value) { // 取最小值向下取整为最小刻度
				let num = 1
				const len = value.min.toString().length
				if (len > 2) {
					num = 10 * (len - 2)
				} else if (len === 2) {
					num = 10 * (len - 1)
				}
				return Math.floor(value.min / num) * num
			},
			max: function(value) { // 取最大值向上取整为最大刻度
				let num = 1
				const len = value.max.toString().length
				if (len > 2) {
					num = 10 * (len - 2)
				} else if (len === 2) {
					num = 10 * (len - 1)
				}
				return Math.ceil(value.max / num) * num
			},
			// splitNumber: 40,
			scale: true,
			splitLine: { // 网格线
				lineStyle: {
					type: 'dashed' // 设置网格线类型 dotted：虚线   solid:实线
				},
				show: true // 隐藏或显示
			}
		},


	]
}

// 處理result數據
const dealResult = (result) => {
	const xAxisData = result.times || []
	const extraSeriesData = result.seriesColor || []

	let legendList = []
	let seriesData = {}

	legendList = result.list || []
	if (result.series) {
		seriesData = result.series || {}

		for (let key in result.series) {
			legendList.map((item, index) => {
				if (item === key) {
					seriesData[key] = result.series[key]
					seriesData['状态'] = result.series['状态']
				}
			})
		}

	} else {
		seriesData = result.records || {}
	}


	const pList = seriesData['状态'] || []

	return {
		xAxisData,
		seriesData,
		extraSeriesData,
		legendList,
		pList
	}
}

// 获取options
export const getOption = (result, label) => {

	const {
		xAxisData,
		seriesData,
		extraSeriesData,
		legendList,
		pList
	} = dealResult(result, label)

	let val = ''
	let series = []

	const {
		xAxis,
		dataZoom
	} = getxAxis(xAxisData, pList)
	let lineSeries = getLineSeries(seriesData)
	let extraSeries = getExtraSeries(extraSeriesData, xAxisData)

	series = series.concat(lineSeries)
	series = series.concat(extraSeries)

	console.log('ser', series)
	return {
		background: '#fff',
		title: {
			text: '煅烧氮氧化物实时数据分析',
			textStyle: {
				align: 'center',
				color: '#000',
				fontSize: 20,
			},
			top: '0%',
			left: 'center',
		},
		xAxis: xAxis,
		yAxis: getyAxis(),
		dataZoom: dataZoom,
		legend: {
			orient: 'horizontal',
			x: 'right', // 可设定图例在左、右、居中
			y: 'bottom', // 可设定图例在上、下、居中
			padding: [0, 100, 50, 0], // 可设定图例[距上方距离，距右方距离，距下方距离，距左方距离]
			itemGap: 10
			// itemWidth: 5,
		},
		grid: [{
				left: 80,
				right: 80,
				height: '35%'
			},
			{

				left: 55,
				right: 80,
				bottom: '25%',
				height: '35%',
				top: '54%',

				containLabel: true
			},


		],
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'cross',
				animation: false
			}
		},
		// tooltip: {
		// 	trigger: 'axis',
		// 	enterable: true,
		// 	extraCssText: 'max-height:400px;overflow:auto;font-size:12px;'
		// 	// formatter: function (params) {
		// 	//     console.log("formatter",params)
		// 	//     chooseList.value = params
		// 	//     return ""
		// 	// }
		// },
		toolbox: {
			show: false,
			feature: {
				dataZoom: {
					yAxisIndex: 'none'
				},
				restore: {},
				saveAsImage: {}
			}
		},
		axisPointer: {
			link: [{
				xAxisIndex: 'all'
			}]
		},
		series: series
	}
}